/**
 * 
 */
package com.tjtt.tdm.codeReview.service.impl;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.drools.lang.DRLParser.init_key_return;

import bsh.Console;

import com.sun.org.apache.bcel.internal.generic.NEW;
import com.tjtt.tdm.base.PageResult;
import com.tjtt.tdm.base.StringUtil;
import com.tjtt.tdm.codeReview.dao.ICodeReviewDao;
import com.tjtt.tdm.codeReview.service.ICodeReviewService;
import com.tjtt.tdm.exception.ServiceException;
import com.tjtt.tdm.flow.bean.FlowDetailed;
import com.tjtt.tdm.flow.bean.FlowTask;
import com.tjtt.tdm.flow.bean.FlowTaskInfo;
import com.tjtt.tdm.flow.bean.Flowtype;
import com.tjtt.tdm.form.bean.TdmAnnex;
import com.tjtt.tdm.form.dao.IAutoDao;
import com.tjtt.tdm.form.dao.IAutoFormDao;



/**
 * @author 张海娜
 *
 */
public class CodeReviewServiceImpl  implements ICodeReviewService{
    private ICodeReviewDao codeReviewDao;
    private IAutoFormDao autoFormDao;
    private IAutoDao autoDao;
    
    
    /**
	 * 根据流程实例id获得实体类
	 * @return
	 * @throws IOException
	 * @throws ServiceException String
	 * @author 张海娜
	 * @date 2013-2-25 下午02:16:54
	 */
	public Flowtype findFileNameById(Long id) 
	{
		return codeReviewDao.findFileNameById(id);
	}
	public String saveFlowTask(FlowTask flowTask)
	{
		return codeReviewDao.saveFlowTask(flowTask);
	}

	public void updateFlowTask(FlowTask flowTask){
		codeReviewDao.updateFlowTask(flowTask);
	}
	
	/**
	 * 
	 * 描述：流程详细信息
	 * @author wangxd
	 * @date Jul 1, 2015 10:51:08 AM
	 * @return void
	 * @throw
	 */
	public void saveFlowDetailed(FlowDetailed flowDetailed)
	{
		codeReviewDao.saveFlowDetailed(flowDetailed);
	}
	public FlowTask getFlowTaskById(Long ids)
	{
		return codeReviewDao.getFlowTaskById(ids);
	}
	public void saveFlowTaskInfo(FlowTaskInfo flowTaskInfo)
	{
		codeReviewDao.saveFlowTaskInfo(flowTaskInfo);
	}
	public List queryListBySql(String sql){
		return codeReviewDao.queryListBySql(sql);
	}
	public FlowTaskInfo getFlowTaskInfoById(Long ids)
	{
		return codeReviewDao.getFlowTaskInfoById(ids);
	}
	public void updateFlowTaskInfo(FlowTaskInfo flowTaskInfo)
	{
		codeReviewDao.updateFlowTaskInfo(flowTaskInfo);
	}
	/**
	 * 
	 * 描述：常规检测流程 生成样品编号
	 * @author wangxd
	 * @date Jul 14, 2015 11:51:30 AM
	 * @return void
	 * @throw
	 */
	public void shengchengSampleNo(String ids,String shibiehao){
		String sampleNo ="";                    //样品编号规则 CY-15-0030
		String[] allIds = ids.split(",");
		String[] allMsg = shibiehao.split("-"); //委托识别号：WT15-CY-0030
		sampleNo = allMsg[1]+"-"+allMsg[0].replace("WT", "")+"-"+allMsg[2];
		
//		//单样品时，生成CY-15-0030，与识别号一致
//		if(allIds.length==1){
//			//首先删除已生成的样品编号
//			String  deleteSql = " delete from gwm_samplenoflow  t where t.t_sampleid="+ids;
//			autoFormDao.executeSql(deleteSql);
//			
//			//向gwm_sampleflow表中插入数据
//			String sql = "insert into gwm_samplenoflow (id, t_sampleno, t_sampleid) values  (SEQ_GWM_SAMPLENOFLOW.nextVal,'"+sampleNo+"', "+ids+")";
//	        autoDao.executeSql(sql);
//	        //修改样品表中的样品编号
//	        String updateSql = "   update gwm_sample set  t_sampleno = '"+sampleNo+"' where id ="+ids;
//	        autoDao.executeSql(updateSql);
//		}
//		else{
			//首先删除已生成的样品编号
			String  deleteSql = " delete from gwm_samplenoflow  t where t.t_sampleid  in("+ids+")";
			autoFormDao.executeSql(deleteSql);
			
			//多样品时，第一行样品数量为3则生成CY-15-0030-01/03，第二行样品数量为2则生成CY-15-0030-04/05,第三行样品数量为1则生成CY-15-0030-06
			int num = 0;
			int startSampleNum = 0 ;
			int xuhao = 0 ;

			for(int i=0;i<allIds.length;i++){
				String lastSampleNo =sampleNo;       
				//查询样品的数量
				String sampleSql = " select t.t_samplecount from gwm_sample t where t.id ="+allIds[i];  
				List sampleList =  autoFormDao.getDatabySql(sampleSql);
				if(null!=sampleList&&sampleList.size()>0&&null!=sampleList.get(0)){
					String samplenum ="";
					Long sampleCount = Long.parseLong(sampleList.get(0).toString());
					
//					for(int j=0;j<sampleCount;j++){
//					    num++;
//					    
//					    if(String.valueOf(num).length()==1){
//					    	samplenum = "00"+num;
//					    	if(j==0){
//					    	  startSampleNum ="00"+num;
//					    	}
//					    }
//					    else if(String.valueOf(num).length()==2){
//					    	samplenum = "0"+num;
//					    	if(j==0){
//					    	  startSampleNum ="0"+num;
//					    	}
//					    }
//					    else if(String.valueOf(num).length()==3){
//					    	samplenum = String.valueOf(num);
//					    	if(j==0){
//					    	  startSampleNum = String.valueOf(num);
//					    	}
//					    }
//					//向gwm_sampleflow表中插入数据
//						String sql = "insert into gwm_samplenoflow (id, t_sampleno, t_sampleid) values  (SEQ_GWM_SAMPLENOFLOW.nextVal,'"+sampleNo+"-"+samplenum+"', "+allIds[i]+")";
//				        autoDao.executeSql(sql);
//					}
					
					num = sampleCount.intValue();
					startSampleNum = xuhao ;
					xuhao = startSampleNum + num ;
					String sql = "insert into gwm_samplenoflow (id, t_sampleno, t_sampleid) select SEQ_GWM_SAMPLENOFLOW.nextVal,'"+sampleNo+"-' || lpad(rownum +"+ startSampleNum +",3,'0'), "+allIds[i]+" from dual connect by rownum<=" + num;
			        autoDao.executeSql(sql);
					
//					if(String.valueOf(num).length()==1){
//						xuhao ="00"+num;	
//					}
//					else if(String.valueOf(num).length()==2){
//						xuhao ="0"+num;	
//					}
//					else if(String.valueOf(num).length()==3){
//					//	xuhao ="00"+num;	
//				//	}
//				//	else{
//						xuhao = String.valueOf(num);
//					}
					
					String start =  "000" + String.valueOf(startSampleNum + 1);
					String end =  "000" + String.valueOf(xuhao);
					if(sampleCount==1){
						lastSampleNo = lastSampleNo +"-"+ end.substring(end.length()-3) ;
					}else{
						lastSampleNo = lastSampleNo +"-"+ start.substring(start.length()-3) + "/"+end.substring(end.length()-3);
					}
					//修改样品表中的样品编号
			        String updateSql = "   update gwm_sample set  t_sampleno = '"+lastSampleNo+"' where id ="+allIds[i];
			        autoDao.executeSql(updateSql);
				}
			}
//		}
	}
	
	/**
	 * 
	 *方法描述:查询原始记录模版
	 *@author: wangxd
	 *@date: 2014-3-10下午12:15:11
	 *@param filterMap
	 *@return
	 */
	public PageResult queryFilemodeList(Map filterMap)
	{
		return codeReviewDao.queryFilemodeList(filterMap);
	}
	/**
	 * 
	 * 描述：保存原始记录
	 * @author wangxd
	 * @date Jul 16, 2015 5:22:36 PM
	 * @return String
	 * @throw
	 */
	public String saveysl(String ysjlpath,String filemodeId,String modetype,String ids,String sfqianming ){
		String flag = "true";
		String sfqianmingValue = "";
		if("0".equals(sfqianming)){
			sfqianmingValue = " ,t.t_sfdianziqianm =0 " ;
		}
		String updateSql = " update gwm_item_sample t set t.t_ysjlfilepath='"+ysjlpath+"',t.t_filemodeid='"+filemodeId+"',t.t_mobantype='"+modetype+"' "+sfqianmingValue+" where t.id in ("+ids+")";
		autoFormDao.executeSql(updateSql);
		return flag;
	}
	/**
	 * 
	 * 描述：查询原始记录对应的标签
	 * @author wangxd
	 * @date Aug 5, 2015 5:16:45 PM
	 * @return List
	 * @throw
	 */
	public List queryFlowItemByFileModeId(Long fileModeId,String bqType)
	{
		return codeReviewDao.queryFlowItemByFileModeId(fileModeId, bqType);
	}
	public PageResult queryReportFilemodeList(Map filterMap)
	{
		return codeReviewDao.queryReportFilemodeList(filterMap);
	}
	public TdmAnnex queryTdmAnnexById(Long id) 
	{
		return codeReviewDao.queryTdmAnnexById(id);
	}
	/**
	 * 
	 * 描述：样品接收
	 * @author wangxd
	 * @date Aug 21, 2015 5:02:52 PM
	 * @return PageResult
	 * @throw
	 */
	public PageResult queryGwmSampleManageList(Map filterMap)
	{
		return codeReviewDao.queryGwmSampleManageList(filterMap);
	}
	
	/**
	 * 
	 * 描述：样品接收 --样品详细
	 * @author wangxd
	 * @date Aug 21, 2015 5:02:52 PM
	 * @return PageResult
	 * @throw
	 */
	public PageResult querySampleDetailBytaskId(Map filterMap)
	{
		return codeReviewDao.querySampleDetailBytaskId(filterMap);
	}
	/**
	 * 
	 * 描述：样品接收
	 * @author wangxd
	 * @date Aug 25, 2015 9:27:31 AM
	 * @return void
	 * @throw
	 */
	public void savesampleAccept(String msg){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String riqi = sdf.format(new Date());
		String userNm = ServletActionContext.getRequest().getSession().getAttribute("username").toString();
		if(!"".equals(msg)){
			String[] allMsg = msg.split("&@&");
			for(int i=0;i<allMsg.length;i++){
				String[] values = allMsg[i].split("#@#");
				String id = values[0];
				Long jieshounum = null;
				String remark= "";
				if(values.length>1&&!StringUtil.isEmpty(values[1])){
					jieshounum = Long.parseLong(values[1].toString());  //接收数量
				}
				if(values.length>2&&!StringUtil.isEmpty(values[2])){
					remark = values[2].toString();
				}
				String sql = "update gwm_sample t set t.t_jieshounum="+jieshounum+" ,t.t_jieshouren='"+userNm+"',t.t_jieshoutime='"+riqi+"',t.t_jieshouremark='"+remark+"',t.t_sfguihuan ='0' where t.id = "+id;
			    autoFormDao.executeSql(sql);
			    
			    //判断同一个taskid的所有样品是否全部接收  如果全部接受完 则修改gwm_sample_manage表中的samplestate为0
			    String querySql = "select  t.id from gwm_sample t where  t.T_SFGUIHUAN is null and t.t_taskid = (select s.t_taskid from gwm_sample s where s.id = "+id+") ";
			    List list = autoFormDao.getDatabySql(querySql);
			    if(null!=list&&list.size()>0&&null!=list.get(0)){
			    	
			    }else{
			    	String updateSql = " update gwm_sample_manage t set t.t_samplestate=0 where t.t_taskid = (select s.t_taskid from gwm_sample s where s.id = "+id+") ";
			        autoFormDao.executeSql(updateSql);
			    }
			}
		}
	}
	/**
	 * 
	 * 描述：样品归还--保存
	 * @author wangxd
	 * @date Aug 25, 2015 6:20:34 PM
	 * @return void
	 * @throw
	 */
	public void saveReturnSample(String msg){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String riqi = sdf.format(new Date());
		String userNm = ServletActionContext.getRequest().getSession().getAttribute("username").toString();
		if(!"".equals(msg)){
			String[] allMsg = msg.split("&@&");
			for(int i=0;i<allMsg.length;i++){
				String[] values = allMsg[i].split("#@#");
				String id = values[0];
				Long guihuancount = null;  //归还数量
				String lingyongren = "";  //领用人
				String remark = "";       //备注
				
				if(values.length>1){
					guihuancount =  Long.parseLong(values[1].toString());
				}
				if(values.length>2){
					lingyongren = values[2].toString();
				}
				if(values.length>3){
					remark = values[3].toString();
				}
				
				String sql = "update gwm_sample t set t.t_guihuannum="+guihuancount+" ,t.t_lingyongren='"+lingyongren+"',t.t_guihuanren='"+userNm+"',t.t_guihuantime='"+riqi+"',t.t_guihuanremark='"+remark+"',t.t_sfguihuan ='1' where t.id = "+id;
				autoFormDao.executeSql(sql);
				
				//判断同一个taskid的 退样的 样品 是否全部接收  如果全部归还完 则修改gwm_sample_manage表中的samplestate为1
				String querySql = "select  t.id from gwm_sample t where  t.T_SFGUIHUAN =0 and t.t_samplechuli='退样' and t.t_taskid = (select s.t_taskid from gwm_sample s where s.id = "+id+") ";
				List list = autoFormDao.getDatabySql(querySql);
				if(null!=list&&list.size()>0&&null!=list.get(0)){
					
				}else{
					String updateSql = " update gwm_sample_manage t set t.t_samplestate=1 where t.t_taskid = (select s.t_taskid from gwm_sample s where s.id = "+id+") ";
					autoFormDao.executeSql(updateSql);
				}
			}
		}
	}
	public void deleteFlowTaskInfoByTaskid(String taskid) 
	{
		codeReviewDao.deleteFlowTaskInfoByTaskid(taskid);
	}
	public List<FlowTaskInfo> getFlowTaskInfoByTaskid(String flowtaskid)
	{
		return codeReviewDao.getFlowTaskInfoByTaskid(flowtaskid);
	}
	public List queryTdmPrintreportmodeItem(String fileModeId, String bqType){
		return codeReviewDao.queryTdmPrintreportmodeItem(fileModeId, bqType);
	}
	 /** 
	 *方法描述:根据打印模版ID查询对应的标签
	 *@author: wangxd
	 *@date: 2013-11-15下午02:14:40
	 *@param fileModeId
	 *@return
	 */
	public List queryPrintbiaoqNameById(Long fileModeId,String bqType)
	{
		return codeReviewDao.queryPrintbiaoqNameById(fileModeId, bqType);
	}
	
	public ICodeReviewDao getCodeReviewDao() {
		return codeReviewDao;
	}
	public void setCodeReviewDao(ICodeReviewDao codeReviewDao) {
		this.codeReviewDao = codeReviewDao;
	}
	public IAutoDao getAutoDao() {
		return autoDao;
	}
	public void setAutoDao(IAutoDao autoDao) {
		this.autoDao = autoDao;
	}
	public IAutoFormDao getAutoFormDao() {
		return autoFormDao;
	}
	public void setAutoFormDao(IAutoFormDao autoFormDao) {
		this.autoFormDao = autoFormDao;
	}
    
}
